<?php
include('admin_advert.php');
/**
 * Created by PhpStorm.
 * User: tsa
 * Date: 17/12/2013
 * Time: 11:34 πμ
 */
function detect_column($con, $table, $column, $flag)
{
    $sql = "SHOW COLUMNS FROM $table LIKE '$column'";
    $result = mysqli_query($con, $sql);
    if(mysqli_num_rows($result) == $flag)
        return true;
    else
        return false;
}

if ($_GET['id_ad'] == "0")
{
    if ($_POST['click'] == "1")
    {
        $sql_q = 'SELECT * FROM field WHERE name="'.$_POST['name'].'"';
        $result = mysqli_query($con, $sql_q);
        $row = mysqli_fetch_array($result);
        $_name="";
        $__name="";
        //echo $sql_q.'<br>';
        if (!empty($row['name']))
        {
            $_name='Το name υπάρχει! Η εισαγωγή δεν έγινε.';
        }
        $sql_q = 'SELECT * FROM field WHERE divname="'.$_POST['divname'].'"';
        $result = mysqli_query($con, $sql_q);
        $row = mysqli_fetch_array($result);
        //echo $sql_q.'<br>';
        if (!empty($row['divname']))
        {
            $__name='Το divname υπάρχει! Η εισαγωγή δεν έγινε.';
        }
        $tmp = trim($_POST['name']);
        if (empty($tmp))
        {
            $_name = 'Δεν επιτρέπονται τα κενά name!';
        }
        $tmp = trim($_POST['divname']);
        if (empty($tmp))
        {
            $__name = 'Δεν επιτρέπονται τα κενά divname!';
        }
        if (empty($_name) and empty($__name))
        {
        $sql_q = 'INSERT INTO field (`id`,`name`,`divname`, `parameters`) VALUES (NULL,"'.$_POST['name'].'","'.$_POST['divname'].'","'.$_POST['parameters'].'")';
        $result = mysqli_query($con, $sql_q);

        $sql_q= 'SELECT * FROM field where name="'.$_POST['name'].'" AND divname="'.$_POST['divname'].'" AND parameters="'.$_POST['parameters'].'"';
        $result = mysqli_query($con,$sql_q);
        $row = mysqli_fetch_array($result);
        $param = 'var'.intval($row['id']);
        if ($_POST['parameters'] <> "NULL")
        {
            if (detect_column($con, 'field_data',$param,0))
            {
                if ($_POST['parameters'] == "TEXT")
                {
                $sql_q = 'ALTER TABLE field_data ADD '.$param.' VARCHAR(100) DEFAULT ""';
                }
                elseif ($_POST['parameters'] == "TEXTAREA")
                {
                $sql_q = 'ALTER TABLE field_data ADD '.$param.' VARCHAR(255) DEFAULT ""';
                }
                elseif ($_POST['parameters'] == "MEDIA")
                {
                    $sql_q = 'ALTER TABLE field_data ADD '.$param.' BLOB';
                }
                else
                {
                $sql_q = 'ALTER TABLE field_data ADD '.$param.' INT(11) DEFAULT 0';
                }
                echo $sql_q;
                $result = mysqli_query($con, $sql_q);
            }
        }
        }
        echo $_name.'<br>'.$__name;
    }

    $click = "0";
    $_POST["click"] = "0";


    $form = '<h4>Εισαγωγή νέου πεδίου</h4>';
    $form = $form . '<form id="form_category_insert" method="post">';
    $form = $form . '<label> Name</label>';
    $form = $form . '<input name="name" type="text" value=""/>';
    $form = $form . '<label> Divname</label>';
    $form = $form . '<input name="divname" type="text" value=""/>';
    $form = $form . '<label> Parameteres</label>';
    $form = $form . '<select name="parameters"/>';
    $form = $form . '<option selected value="NULL">NULL</option>';
    $form = $form . '<option value="TEXT"> TEXT FIELD</option>';
    $form = $form . '<option value="RADIOBOX"> RADIOBOX FIELD</option>';
    $form = $form . '<option value="CHECKBOX"> CHECKBOX FIELD</option>';
    $form = $form . '<option value="PASSWORD"> PASSWORD FIELD</option>';
    $form = $form . '<option value="MEDIA"> MEDIA</option>';
    $form = $form . '<option value="DROPDOWNBOX"> DROP DOWN BOX FIELD</option>';
    $form = $form . '<option value="TEXTAREA"> TEXT AREA FIELD</option>';

    $form = $form . '</select>';
    $form = $form . '<input name="click" type="hidden" value="1"/>';
    $form = $form . '<input type="submit" value="Προσθήκη πεδίου"/>';
    $form = $form . '</form>';
    echo $form;
}
elseif ($_GET['id_ad'] == "1") {
    $result = mysqli_query($con, " SELECT * FROM field group by name ");
    echo '<table border="1">';
    while($row = mysqli_fetch_array($result))
    {
        echo '<tr><td>'.$row['id'].'</td><td>'.$row['name'].'</td><td>'.$row['divname'].'</td><td>[ <a href="?page=admin_advert/field_edit&id_ad=2&id='.$row['id'].'">Επεξεργασία</a> ]</td><td>[ <a href="?page=admin_advert/field_edit&id_ad=3&id='.$row['id'].'">Διαγραφή</a> ]</td></tr>';
    }
    echo '</table>';
}
else
{
    echo "<br><h2>PAGE NOT FOUND</h2>";
}
?>